package com.rrd.hive.udf;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Description(
        name = "json_auto_parse_plus",
        value = "_FUNC_(json, path, split='-|-') - Evaluates a list xpath expression",
        extended = "Example:\n> SELECT _FUNC_('{\"a\": {\"b\": {\"c\": 1, \"e\": 2, \"d\": 3}, \"d\": 1}, \"aa\": {\"bb\": 1}}','a');"
)

public class JsonAutoParsePlusUDF extends UDF {


    public JsonAutoParsePlusUDF() {
    }


    public List<Text> evaluate(String json, String path, String split) {
        ArrayList<Text> result = new ArrayList<>();
        if (json == null || path == null) {
            return result;
        }
        JSONObject obj = JSON.parseObject(json);
        if (path.length() > 0) {
            for (String node : path.split("/")) {
                if (obj == null) {
                    return result;
                }
                obj = obj.getJSONObject(node);
            }
        }
        if (obj == null) {
            return result;
        }
        for (Map.Entry<String, Object> entry : obj.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof JSONObject) {
                for (Map.Entry<String, Object> v : ((JSONObject) value).entrySet()) {
                    if (v.getValue() instanceof JSONArray) {
                        JSONArray vv = (JSONArray) v.getValue();
                        for (int i = 0; i < vv.size(); i++) {
                            result.add(new Text(entry.getKey() + split + v.getKey() + split + vv.getString(i)));
                        }
                    } else {
                        result.add(new Text(entry.getKey() + split + v.getKey() + split + v.getValue()));
                    }
                }
            } else {
                result.add(new Text(entry.getKey() + split + "null" + split + entry.getValue()));
            }
        }
        return result;

    }

    public List<Text> evaluate(String json, String path) {
        String split = "-|-";
        return evaluate(json, path, split);
    }

    public List<Text> evaluate(String json) {
        String split = "-|-";
        String path = "";
        return evaluate(json, path, split);
    }


    public static void main(String[] args) {
//        String json = "{\"a\": {\"b\": {\"c\": {\"d\": [1, 2, 3]}}}}";
//        List<Text> l1 = new JsonAutoParsePlusUDF().evaluate(json, "f");
//        for (Text a : l1) {
//            System.out.println(a);
//        }
        String a = "gCyREKr2lqqBocA766VZwcNLyVJUFYTNiS08LRRI7+4pXGDIvJ/b3szCpWU+5vpJj04w2Ca8q09d\\njwO4u33vZJmfEBahoIMjHUUtRRjvrJM=";
        Map<String, String> c = new HashMap<>();
        c.put("a", "gCyREKr2lqqBocA766VZwcNLyVJUFYTNiS08LRRI7+4pXGDIvJ/b3szCpWU+5vpJj04w2Ca8q09d\\njwO4u33vZJmfEBahoIMjHUUtRRjvrJM=");
        String b = c.get("a").replace("\n", " ");
        System.out.println(b);
    }


}